
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>JavaScript之防抖Debounce和节流Throttle · 记录技术文档内容</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="zhengyuliang">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-sectionx/sectionx.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-plus/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="JavaScript数组求并集，交集和差集.html" />
    
    
    <link rel="prev" href="JS开发必须知道的41个技巧.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">CSS</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../CSS/18种CSS命名和书写规范.html">
            
                <a href="../CSS/18种CSS命名和书写规范.html">
            
                    
                    18种CSS命名和书写规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../CSS/灵活运用CSS开发技巧.html">
            
                <a href="../CSS/灵活运用CSS开发技巧.html">
            
                    
                    灵活运用CSS开发技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../CSS/自适应 flex grid.html">
            
                <a href="../CSS/自适应 flex grid.html">
            
                    
                    自适应 Flex Grid
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">HTML</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../HTML/浏览器里的本地数据库：IndexedDB.html">
            
                <a href="../HTML/浏览器里的本地数据库：IndexedDB.html">
            
                    
                    浏览器里的本地数据库：IndexedDB
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Vue</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../Vue/7个有用的Vue开发技巧.html">
            
                <a href="../Vue/7个有用的Vue开发技巧.html">
            
                    
                    7个有用的Vue开发技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../Vue/Vue this.$router.push、replace、go的区别.html">
            
                <a href="../Vue/Vue this.$router.push、replace、go的区别.html">
            
                    
                    Vue This.$Router.Push、Replace、Go的区别
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../Vue/Vue 的父组件和子组件生命周期钩子函数执行顺序？.html">
            
                <a href="../Vue/Vue 的父组件和子组件生命周期钩子函数执行顺序？.html">
            
                    
                    Vue 的父组件和子组件生命周期钩子函数执行顺序？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../Vue/Vue.set和this.$set应用的场景.html">
            
                <a href="../Vue/Vue.set和this.$set应用的场景.html">
            
                    
                    Vue.Set和This.$Set应用的场景
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="../Vue/vue高级技巧.html">
            
                <a href="../Vue/vue高级技巧.html">
            
                    
                    Vue高级技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="../Vue/从 0 开始手把手带你搭建一套规范的 Vue3.x 项目工程环境.html">
            
                <a href="../Vue/从 0 开始手把手带你搭建一套规范的 Vue3.x 项目工程环境.html">
            
                    
                    从 0 开始手把手带你搭建一套规范的 Vue3.X 项目工程环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="../Vue/浅析 vue-router 源码和动态路由权限分配.html">
            
                <a href="../Vue/浅析 vue-router 源码和动态路由权限分配.html">
            
                    
                    浅析 Vue Router 源码和动态路由权限分配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="../Vue/父组件监听到子组件的生命周期.html">
            
                <a href="../Vue/父组件监听到子组件的生命周期.html">
            
                    
                    父组件监听到子组件的生命周期
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="../Vue/理解Vue.mixin，利用Vue.mixin正确的偷懒.html">
            
                <a href="../Vue/理解Vue.mixin，利用Vue.mixin正确的偷懒.html">
            
                    
                    理解Vue.Mixin，利用Vue.Mixin正确的偷懒
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">JavaScript</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="5个技巧让你更好的编写 JavaScript(ES6">
            
                <span>
            
                    
                    5个技巧让你更好的编写 JavaScript(ES6) 中条件语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="5种常见函数的写法和调用方式.html">
            
                <a href="5种常见函数的写法和调用方式.html">
            
                    
                    5种常见函数的写法和调用方式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="7道javascript题，让你理解基本的javascript.html">
            
                <a href="7道javascript题，让你理解基本的javascript.html">
            
                    
                    7道Javascript题，让你理解基本的Javascript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="Array.from(">
            
                <span>
            
                    
                    Array.From() 五个超好用的用途
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="ES6汇总资料.html">
            
                <a href="ES6汇总资料.html">
            
                    
                    ES6汇总资料
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="JS开发必须知道的41个技巧.html">
            
                <a href="JS开发必须知道的41个技巧.html">
            
                    
                    JS开发必须知道的41个技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="4.7" data-path="JavaScript之防抖Debounce和节流Throttle.html">
            
                <a href="JavaScript之防抖Debounce和节流Throttle.html">
            
                    
                    JavaScript之防抖Debounce和节流Throttle
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="JavaScript数组求并集，交集和差集.html">
            
                <a href="JavaScript数组求并集，交集和差集.html">
            
                    
                    JavaScript数组求并集，交集和差集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.9" data-path="JavaScript的优雅写法.html">
            
                <a href="JavaScript的优雅写法.html">
            
                    
                    JavaScript的优雅写法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.10" data-path="axios知识点.html">
            
                <a href="axios知识点.html">
            
                    
                    Axios知识点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.11" data-path="javascript核心知识.html">
            
                <a href="javascript核心知识.html">
            
                    
                    Javascript核心知识
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.12" data-path="this、apply、call、bind.html">
            
                <a href="this、apply、call、bind.html">
            
                    
                    This、Apply、Call、Bind
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.13" data-path="前端js常用的60余种工具方法.html">
            
                <a href="前端js常用的60余种工具方法.html">
            
                    
                    前端Js常用的60余种工具方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.14" data-path="前端异常的捕获与处理.html">
            
                <a href="前端异常的捕获与处理.html">
            
                    
                    前端异常的捕获与处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.15" data-path="在 JavaScript 中交换值的 10 种方法.html">
            
                <a href="在 JavaScript 中交换值的 10 种方法.html">
            
                    
                    在 JavaScript 中交换值的 10 种方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.16" data-path="常用的前端JavaScript方法封装.html">
            
                <a href="常用的前端JavaScript方法封装.html">
            
                    
                    常用的前端JavaScript方法封装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.17" data-path="异步神器Async-await介绍与填坑.html">
            
                <a href="异步神器Async-await介绍与填坑.html">
            
                    
                    异步神器Async Await介绍与填坑
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.18" data-path="灵活运用JS开发技巧.html">
            
                <a href="灵活运用JS开发技巧.html">
            
                    
                    灵活运用JS开发技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.19" data-path="编写高质量可维护的代码：一目了然的注释.html">
            
                <a href="编写高质量可维护的代码：一目了然的注释.html">
            
                    
                    编写高质量可维护的代码：一目了然的注释
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.20" data-path="编写高质量可维护的代码：优化逻辑判断.html">
            
                <a href="编写高质量可维护的代码：优化逻辑判断.html">
            
                    
                    编写高质量可维护的代码：优化逻辑判断
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.21" data-path="编写高质量可维护的代码：优雅命名.html">
            
                <a href="编写高质量可维护的代码：优雅命名.html">
            
                    
                    编写高质量可维护的代码：优雅命名
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.22" data-path="编写高质量可维护的代码：数据建模.html">
            
                <a href="编写高质量可维护的代码：数据建模.html">
            
                    
                    编写高质量可维护的代码：数据建模
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Life Exp</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../life_exp/告别平庸的15个小方法.html">
            
                <a href="../life_exp/告别平庸的15个小方法.html">
            
                    
                    告别平庸的15个小方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../life_exp/日志记录.html">
            
                <a href="../life_exp/日志记录.html">
            
                    
                    日志记录
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../life_exp/程序员的发展方向.html">
            
                <a href="../life_exp/程序员的发展方向.html">
            
                    
                    程序员的发展方向
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../message/">
            
                <a href="../message/">
            
                    
                    Message
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Technology</li>
        
        
    
        <li class="chapter " data-level="6.1" data-path="../technology/WebSocket.html">
            
                <a href="../technology/WebSocket.html">
            
                    
                    WebSocket
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="../technology/npm常用命令大全.html">
            
                <a href="../technology/npm常用命令大全.html">
            
                    
                    Npm常用命令大全
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="../technology/智能照明项目说明.html">
            
                <a href="../technology/智能照明项目说明.html">
            
                    
                    智能照明项目说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="../technology/规范化git的提交信息.html">
            
                <a href="../technology/规范化git的提交信息.html">
            
                    
                    规范化Git的提交信息
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Tutorial</li>
        
        
    
        <li class="chapter " data-level="7.1" data-path="../tutorial/Esmap地图详解.html">
            
                <a href="../tutorial/Esmap地图详解.html">
            
                    
                    Esmap地图详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="../tutorial/npm镜像切换.html">
            
                <a href="../tutorial/npm镜像切换.html">
            
                    
                    Npm镜像切换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="../tutorial/发布npm的教程.html">
            
                <a href="../tutorial/发布npm的教程.html">
            
                    
                    发布Npm的教程
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >JavaScript之防抖Debounce和节流Throttle</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div class="search-plus" id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <html><head></head><body><div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><ul><ul><li><span class="title-icon "></span><a href="#&#x4E00;&#x3001;&#x80CC;&#x666F;"><b>1.1.1. </b>&#x4E00;&#x3001;&#x80CC;&#x666F;</a></li><li><span class="title-icon "></span><a href="#&#x4E8C;&#x3001;&#x9632;&#x6296;&#xFF08;debounce&#xFF09;"><b>1.1.2. </b>&#x4E8C;&#x3001;&#x9632;&#x6296;&#xFF08;Debounce&#xFF09;</a></li><li><span class="title-icon "></span><a href="#&#x4E09;&#x3001;&#x8282;&#x6D41;&#xFF08;throttle&#xFF09;"><b>1.1.3. </b>&#x4E09;&#x3001;&#x8282;&#x6D41;&#xFF08;Throttle&#xFF09;</a></li><li><span class="title-icon "></span><a href="#requestanimationframe"><b>1.1.4. </b>requestAnimationFrame</a></li><li><span class="title-icon "></span><a href="#&#x56DB;&#x3001;&#x7ED3;&#x8BBA;"><b>1.1.5. </b>&#x56DB;&#x3001;&#x7ED3;&#x8BBA;</a></li></ul></ul></ul></div><a href="#" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h4 id="javascript&#x4E4B;&#x9632;&#x6296;&#xFF08;debounce&#xFF09;&#x548C;&#x8282;&#x6D41;&#xFF08;throttle&#xFF09;"><a name="javascript&#x4E4B;&#x9632;&#x6296;&#xFF08;debounce&#xFF09;&#x548C;&#x8282;&#x6D41;&#xFF08;throttle&#xFF09;" class="anchor-navigation-ex-anchor" href="#javascript&#x4E4B;&#x9632;&#x6296;&#xFF08;debounce&#xFF09;&#x548C;&#x8282;&#x6D41;&#xFF08;throttle&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a>JavaScript&#x4E4B;&#x9632;&#x6296;&#xFF08;Debounce&#xFF09;&#x548C;&#x8282;&#x6D41;&#xFF08;Throttle&#xFF09;</h4>
<h3 id="&#x4E00;&#x3001;&#x80CC;&#x666F;"><a name="&#x4E00;&#x3001;&#x80CC;&#x666F;" class="anchor-navigation-ex-anchor" href="#&#x4E00;&#x3001;&#x80CC;&#x666F;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1.1. &#x4E00;&#x3001;&#x80CC;&#x666F;</h3>
<p>&#x9632;&#x6296;&#x548C;&#x8282;&#x6D41;&#x662F;&#x4E24;&#x79CD;&#x4E0D;&#x540C;&#x7684;&#x63A7;&#x5236;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x6267;&#x884C;&#x6B21;&#x6570;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x5176;&#x76EE;&#x7684;&#x90FD;&#x662F;&#x4E3A;&#x4E86;&#x8282;&#x7EA6;&#x8BA1;&#x7B97;&#x673A;&#x8D44;&#x6E90;&#x3002;
 &#x5F53;&#x6211;&#x4EEC;&#x64CD;&#x4F5C;DOM&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x52A0;&#x4E0A;&#x8282;&#x6D41;&#x6216;&#x8005;&#x9632;&#x6296;&#x5C31;&#x975E;&#x5E38;&#x6709;&#x5FC5;&#x8981;&#xFF0C;&#x56E0;&#x4E3A;&#x4F17;&#x6240;&#x5468;&#x77E5;&#xFF0C;&#x64CD;&#x4F5C;DOM&#x7684;&#x5F00;&#x9500;&#x662F;&#x975E;&#x5E38;&#x5927;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x8981;&#x5C3D;&#x53EF;&#x80FD;&#x51CF;&#x5C11;DOM&#x64CD;&#x4F5C;&#x6B21;&#x6570;&#x3002;</p>
<pre><code class="lang-html">// html
<span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Number of scroll events <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;reset&quot;</span>&gt;</span>Reset<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;counter&quot;</span>&gt;</span>0<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<pre><code class="lang-css">//css
body {
   background: #444444;
   color: white;
    font: 15px/1.51 system, -apple-system, &quot;.SFNSText-Regular&quot;, &quot;San Francisco&quot;, &quot;Roboto&quot;, &quot;Segoe UI&quot;, &quot;Helvetica Neue&quot;, &quot;Lucida Grande&quot;, sans-serif;
   margin:0 auto;
   max-width:600px;
   padding:20px;
   min-height:1000vh; /* 100 times viewport height */
}
#counter {
  position:fixed;
  top:100px;
  left:40%;
  font-size:50px;
}
.reset {
  color:white;
  text-decoration:none;
  border:1px solid white;
  padding:10px 20px;
  background:rgba(0,0,0,0.1);
}
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-comment">//js</span>
<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>;
<span class="hljs-keyword">var</span> $counter = $(<span class="hljs-string">&apos;#counter&apos;</span>);
$(<span class="hljs-built_in">document</span>).ready(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
  $(<span class="hljs-built_in">document</span>).on(<span class="hljs-string">&apos;scroll&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    $counter.html(i);
    i++; 
  });
});

$(<span class="hljs-string">&apos;.reset&apos;</span>).on(<span class="hljs-string">&apos;click&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
  $counter.html(<span class="hljs-string">&apos;&apos;</span>);
  i = <span class="hljs-number">0</span>;
})
</code></pre>
<p> &#x5F53;&#x9F20;&#x6807;&#x6EDA;&#x52A8;&#x6216;&#x8005;&#x62D6;&#x62FD;&#x7684;&#x65F6;&#x5019;&#x53EF;&#x4EE5;&#x8F7B;&#x6613;&#x5730;&#x6BCF;&#x79D2;&#x89E6;&#x53D1;30&#x4E2A;&#x4E8B;&#x4EF6;&#xFF0C;&#x800C;&#x4E14;&#x5728;&#x79FB;&#x52A8;&#x7AEF;&#x6162;&#x901F;&#x6EDA;&#x52A8;&#x53EF;&#x4EE5;&#x8FBE;&#x5230;&#x6BCF;&#x79D2;100&#x6B21;&#xFF0C;&#x8981;&#x662F;&#x8FD9;&#x4E48;&#x77ED;&#x7684;&#x65F6;&#x95F4;&#x5185;&#x6BCF;&#x6B21;&#x90FD;&#x89E6;&#x53D1;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x90A3;&#x6D4F;&#x89C8;&#x5668;&#x5E94;&#x8BE5;&#x4F1A;&#x5361;&#x6B7B;&#x5D29;&#x6E83;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x8FD9;&#x5C31;&#x5F15;&#x51FA;&#x4E86;<strong>&#x9632;&#x6296;</strong>&#x548C;<strong>&#x8282;&#x6D41;</strong>&#x3002; </p>
<h3 id="&#x4E8C;&#x3001;&#x9632;&#x6296;&#xFF08;debounce&#xFF09;"><a name="&#x4E8C;&#x3001;&#x9632;&#x6296;&#xFF08;debounce&#xFF09;" class="anchor-navigation-ex-anchor" href="#&#x4E8C;&#x3001;&#x9632;&#x6296;&#xFF08;debounce&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1.2. &#x4E8C;&#x3001;&#x9632;&#x6296;&#xFF08;Debounce&#xFF09;</h3>
<p>&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;trigger-area&quot;</span>&gt;</span>Trigger area<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;reset&quot;</span>&gt;</span>Reset<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;visualizations&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>Raw events over time<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;raw-events&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;events&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>Debounced events
  <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;details&quot;</span>&gt;</span> 400ms, trailing<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;debounced-events&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;events&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<pre><code class="lang-css">body {
   background: #444444;
   color: white;
   font: 15px/1.51 system, -apple-system, &quot;.SFNSText-Regular&quot;, &quot;San Francisco&quot;, &quot;Roboto&quot;, &quot;Segoe UI&quot;, &quot;Helvetica Neue&quot;, &quot;Lucida Grande&quot;, sans-serif;
   margin:0 auto;
   max-width:700px;
   padding:20px;
}

.events{
  padding:0px 20px 10px 20px;
  height: 23px;
}
.events span {
  height:17px;
  width:6px;
  display:inline-block;
  border-right:1px solid #111;

}
.events span:last-of-type {
  border:2px solid black;
  border-bottom: 4px solid #AAA;
  border-top: 0px;
  margin-bottom:-17px;
  margin-left:-2px;
}
h2 {
  margin:10px 0 5px 0;
  clear:both;
  font-weight: normal;
  font-size:14px;
  padding:6px 20px;
}

.trigger-area {
  margin: 0;
  display:inline-block;
  width: 200px;
  height:50px;
  border: 1px solid #5ed1ff;
  padding: 28px 0 0 0;
  text-align: center;
  background-color: transparent;
  cursor:pointer;
  font-size:17px;
  -webkit-user-select: none;  /* Chrome  / Safari */
  -moz-user-select: none;     /* Firefox all */
  -ms-user-select: none;      /* IE 10+ */
  user-select: none;          /* Likely future */    
}
.trigger-area.active {
  background:#2F5065;
}
.clickme:hover,
.clickme:active{
  background-color: #333;
}
.clickme:active{
  padding: 4px 5px;
}
.reset {
  display:inline-block;
  width: 120px;
  padding: 10px 0 0 0;
  text-align: center;
  font-size:14px;
  cursor:pointer;
  color:#eee;
}
.visualizations {
  margin-top:10px;
  background:rgba(0,0,0,0.2);
}
.details {
  font-size:13px;
  color:#999;
}

/* stating the obvious: color0 represents our empty color */
.color0 { transparent}

.color1 { background-color: #FFE589}
.color2 { background-color: #B9C6FF}
.color3 { background-color: #99FF7E}
.color4 { background-color: #FFB38A}
.color5 { background-color: #A5FCFF}
.color6 { background-color: #FF8E9B}
.color7 { background-color: #E3FF7E}
.color8 { background-color: #FFA3D8}
.color9 { background-color: #5ca6ff}
.color10 { background-color: #9BFFBB}
</code></pre>
<pre><code class="lang-javascript">$(<span class="hljs-built_in">document</span>).ready(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{

  <span class="hljs-keyword">var</span> $rawDiv = $(<span class="hljs-string">&apos;#raw-events&apos;</span>),
      $debounceDiv = $(<span class="hljs-string">&apos;#debounced-events&apos;</span>),
      $triggerArea = $(<span class="hljs-string">&apos;.trigger-area&apos;</span>),
      initialized = <span class="hljs-literal">false</span>,
      frequency = <span class="hljs-number">100</span>,
      barLength = <span class="hljs-number">0</span>,
      globalColor = <span class="hljs-number">2</span>,
      colorNeedChange = <span class="hljs-literal">false</span>,
      interval_id,
      rawColor = <span class="hljs-number">0</span>,
      debounceColor = <span class="hljs-number">0</span>,
      maxBarLength = <span class="hljs-number">87</span>;

  <span class="hljs-keyword">var</span> drawDebouncedEvent = _.debounce(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">div</span>)</span>{
   debounceColor = globalColor;
  }, frequency*<span class="hljs-number">4</span>, {<span class="hljs-attr">leading</span>:<span class="hljs-literal">false</span>, <span class="hljs-attr">trailing</span>:<span class="hljs-literal">true</span>});


  <span class="hljs-keyword">var</span> changeDebouncedColor = _.debounce(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">div</span>)</span>{
    <span class="hljs-comment">// Change colors, to visualize easier the &quot;group of events&quot; that is reperesenting this debounced event</span>

    globalColor++;
    <span class="hljs-keyword">if</span> (globalColor &gt; <span class="hljs-number">9</span>){
      globalColor = <span class="hljs-number">2</span>;
    } 
  }, frequency*<span class="hljs-number">4</span>, {<span class="hljs-attr">leading</span>:<span class="hljs-literal">false</span>, <span class="hljs-attr">trailing</span>:<span class="hljs-literal">true</span>});


  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">draw_tick_marks</span>(<span class="hljs-params"></span>)</span>{

      <span class="hljs-comment">// every x seconds, draw a tick mark in the bar</span>
      interval_id = setInterval(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
      barLength++;   
      $rawDiv.append(<span class="hljs-string">&apos;&lt;span class=&quot;color&apos;</span> + rawColor + <span class="hljs-string">&apos;&quot; &gt;&apos;</span>);
      $debounceDiv.append(<span class="hljs-string">&apos;&lt;span class=&quot;color&apos;</span> + debounceColor + <span class="hljs-string">&apos;&quot; &gt;&apos;</span>);
      rawColor = <span class="hljs-number">0</span>; <span class="hljs-comment">// make it transparent again</span>
      debounceColor = <span class="hljs-number">0</span>; <span class="hljs-comment">// make it transparent again</span>

      <span class="hljs-keyword">if</span> (barLength &gt; maxBarLength){
        clearInterval(interval_id);
      }

    }, frequency);
  };


  <span class="hljs-comment">// Track Mouse movement or clicks for mobile</span>
  $triggerArea.on(<span class="hljs-string">&apos;click mousemove&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>)</span>{  
    <span class="hljs-keyword">if</span> (!initialized) {
      initialized = <span class="hljs-literal">true</span>;
      draw_tick_marks();
      $(<span class="hljs-keyword">this</span>).addClass(<span class="hljs-string">&apos;active&apos;</span>);
    } 
    rawColor = globalColor;
    drawDebouncedEvent();
    changeDebouncedColor();
  });

  $(<span class="hljs-string">&apos;.reset&apos;</span>).on(<span class="hljs-string">&apos;click&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    initialized = <span class="hljs-literal">false</span>;
    $triggerArea.removeClass(<span class="hljs-string">&apos;active&apos;</span>);
    $rawDiv.empty();
    $debounceDiv.empty();
    barLength = <span class="hljs-number">0</span>;
    clearInterval(interval_id);
  });

});
</code></pre>
<h5 id="&#x5177;&#x4F53;&#x5B9E;&#x73B0;"><a name="&#x5177;&#x4F53;&#x5B9E;&#x73B0;" class="anchor-navigation-ex-anchor" href="#&#x5177;&#x4F53;&#x5B9E;&#x73B0;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5177;&#x4F53;&#x5B9E;&#x73B0;</h5>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">debounce</span>(<span class="hljs-params">func,delay</span>) </span>{
    <span class="hljs-keyword">let</span> timer;
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">...args</span>) </span>{
        <span class="hljs-keyword">if</span> (timer) {
            clearTimeout(timer);
        }
        timer = setTimeout(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
            func.apply(<span class="hljs-keyword">this</span>,<span class="hljs-built_in">arguments</span>);
        },delay)
    }
}
</code></pre>
<p> <a href="https://links.jianshu.com/go?to=https%3A%2F%2Flodash.com%2Fdocs%23debounce" target="_blank">Lodash</a>&#x4E2D;&#x5B9E;&#x73B0; <code>_.debounce</code> &#x548C; <code>_.throttle</code> &#x7684;&#x529F;&#x80FD;&#x5F88;&#x5168;&#x9762;&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#xFF0C;&#x5176;&#x4E2D;&#x7684; <code>throttle</code>&#x51FD;&#x6570;&#x662F;&#x4F7F;&#x7528; <code>_.debounce</code> &#x65B0;&#x589E; maxWait` &#x9009;&#x9879;&#x6765;&#x5B9E;&#x73B0;&#x7684; </p>
<h5 id="resize&#x5B9E;&#x4F8B;"><a name="resize&#x5B9E;&#x4F8B;" class="anchor-navigation-ex-anchor" href="#resize&#x5B9E;&#x4F8B;"><i class="fa fa-link" aria-hidden="true"></i></a>Resize&#x5B9E;&#x4F8B;</h5>
<pre><code class="lang-javascript"><span class="hljs-comment">// Based on http://www.paulirish.com/2009/throttled-smartresize-jquery-event-handler/</span>
$(<span class="hljs-built_in">document</span>).ready(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{

  <span class="hljs-keyword">var</span> $win = $(<span class="hljs-built_in">window</span>);
  <span class="hljs-keyword">var</span> $left_panel = $(<span class="hljs-string">&apos;.left-panel&apos;</span>);
  <span class="hljs-keyword">var</span> $right_panel = $(<span class="hljs-string">&apos;.right-panel&apos;</span>);

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">display_info</span>(<span class="hljs-params">$div</span>) </span>{
    $div.append($win.width() + <span class="hljs-string">&apos; x &apos;</span> + $win.height() +  <span class="hljs-string">&apos;&lt;br&gt;&apos;</span>);
  }

  $(<span class="hljs-built_in">window</span>).on(<span class="hljs-string">&apos;resize&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    display_info($left_panel);
  });

  $(<span class="hljs-built_in">window</span>).on(<span class="hljs-string">&apos;resize&apos;</span>, _.debounce(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    display_info($right_panel);
  }, <span class="hljs-number">400</span>));
});
</code></pre>
<h5 id="&#x8F93;&#x5165;&#x9A8C;&#x8BC1;&#x5B9E;&#x4F8B;"><a name="&#x8F93;&#x5165;&#x9A8C;&#x8BC1;&#x5B9E;&#x4F8B;" class="anchor-navigation-ex-anchor" href="#&#x8F93;&#x5165;&#x9A8C;&#x8BC1;&#x5B9E;&#x4F8B;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x8F93;&#x5165;&#x9A8C;&#x8BC1;&#x5B9E;&#x4F8B;</h5>
<pre><code class="lang-javascript">$(<span class="hljs-built_in">document</span>).ready(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{

  <span class="hljs-keyword">var</span> $statusKey = $(<span class="hljs-string">&apos;.status-key&apos;</span>);
  <span class="hljs-keyword">var</span> $statusAjax = $(<span class="hljs-string">&apos;.status-ajax&apos;</span>);
  <span class="hljs-keyword">var</span> intervalId;

  <span class="hljs-comment">// Fake ajax request. Just for demo</span>
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">make_ajax_request</span>(<span class="hljs-params">e</span>)</span>{
      <span class="hljs-keyword">var</span> that = <span class="hljs-keyword">this</span>;
      $statusAjax.html(<span class="hljs-string">&apos;That\&apos;s enough waiting. Making now the ajax request&apos;</span>);

      intervalId = setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
         $statusKey.html(<span class="hljs-string">&apos;Type here. I will detect when you stop typing&apos;</span>);
         $statusAjax.html(<span class="hljs-string">&apos;&apos;</span>);
         $(that).val(<span class="hljs-string">&apos;&apos;</span>); <span class="hljs-comment">// empty field</span>
      },<span class="hljs-number">2000</span>);
    }

  <span class="hljs-comment">// Event handlers to show information when events are being emitted</span>
    $(<span class="hljs-string">&apos;.autocomplete&apos;</span>)
      .on(<span class="hljs-string">&apos;keydown&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>)</span>{  

        $statusKey.html(<span class="hljs-string">&apos;Waiting for more keystrokes... &apos;</span>);
      clearInterval(intervalId);             
      })


  <span class="hljs-comment">// Display when the ajax request will happen (after user stops typing)</span>
    <span class="hljs-comment">// Exagerated value of 1.2 seconds for demo purposes, but in a real example would be better from 50ms to 200ms</span>
  $(<span class="hljs-string">&apos;.autocomplete&apos;</span>).on(<span class="hljs-string">&apos;keydown&apos;</span>,
       _.debounce(make_ajax_request, <span class="hljs-number">1300</span>));
  });
</code></pre>
<p> &#x5F53;&#x7528;&#x6237;&#x8F93;&#x5165;&#x5E76;&#x4E14;&#x53D1;&#x8D77;<code>Ajax</code>&#x8BF7;&#x6C42;&#x7684;&#x65F6;&#x5019;&#xFF0C;<code>_.debounce</code>&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x5B9E;&#x73B0;&#x9632;&#x6296;&#xFF0C;&#x6BD4;&#x5982;&#x95F4;&#x9694;2&#x79D2;&#x672A;&#x68C0;&#x6D4B;&#x5230;&#x7528;&#x6237;&#x8F93;&#x5165;&#x624D;&#x53D1;&#x8D77;&#x8BF7;&#x6C42;&#x3002; </p>
<h3 id="&#x4E09;&#x3001;&#x8282;&#x6D41;&#xFF08;throttle&#xFF09;"><a name="&#x4E09;&#x3001;&#x8282;&#x6D41;&#xFF08;throttle&#xFF09;" class="anchor-navigation-ex-anchor" href="#&#x4E09;&#x3001;&#x8282;&#x6D41;&#xFF08;throttle&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1.3. &#x4E09;&#x3001;&#x8282;&#x6D41;&#xFF08;Throttle&#xFF09;</h3>
<p> &#x5177;&#x4F53;&#x5B9E;&#x73B0;  --  &#x4F7F;&#x7528;&#x5B9A;&#x65F6;&#x5668; </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> throttle = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">func, delay</span>) </span>{
    <span class="hljs-keyword">var</span> timer = <span class="hljs-literal">null</span>;
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">if</span> (!timer) {
            timer = setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
                func.apply(<span class="hljs-keyword">this</span>,<span class="hljs-built_in">arguments</span>);
                timer = <span class="hljs-literal">null</span>;
            },delay);
        }
    }
}
</code></pre>
<p> &#x53E6;&#x4E00;&#x79CD;&#x5B9A;&#x65F6;&#x5668;&#x5199;&#x6CD5;&#xFF1A; </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> isAllow = <span class="hljs-literal">true</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">throttle</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> fun = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">if</span> (!isAllow)
            <span class="hljs-keyword">return</span>;
        <span class="hljs-keyword">let</span> timer = setTimeout(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;throttle&quot;</span>);
            clearTimeout(timer);
            timer = <span class="hljs-literal">null</span>;
            isAllow = <span class="hljs-literal">true</span>;
        },<span class="hljs-number">1000</span>);
    };
    fun()&#xFF1B;
}
</code></pre>
<p> &#x4E8C;&#x8005;&#x7684;&#x533A;&#x522B;&#x662F;&#x540E;&#x8005;&#x4F7F;&#x7528;&#x4E86;isAllow&#x6807;&#x5FD7;&#x4F4D;&#x6765;&#x5224;&#x65AD;&#x662F;&#x5426;&#x9700;&#x8981;&#x6267;&#x884C;&#x51FD;&#x6570;&#x3002; </p>
<p> &#x6EDA;&#x52A8;&#x6761;&#x5B9E;&#x4F8B; </p>
<p> &#x4E00;&#x4E2A;&#x5341;&#x5206;&#x5E38;&#x89C1;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x5F53;&#x7528;&#x6237;&#x7684;&#x9F20;&#x6807;&#x6EDA;&#x52A8;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x68C0;&#x67E5;&#x9F20;&#x6807;&#x4F4D;&#x7F6E;&#x79BB;&#x5E95;&#x90E8;&#x7684;&#x8DDD;&#x79BB;&#xFF0C;&#x5F53;&#x63A5;&#x8FD1;&#x5E95;&#x90E8;&#x65F6;&#xFF0C;&#x53D1;&#x8D77;<code>Ajax</code>&#x8BF7;&#x6C42;&#x3002; </p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Infinite scrolling throttled<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;item color-1&quot;</span>&gt;</span>Block 1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;item color-2&quot;</span>&gt;</span>Block 2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;item color-3&quot;</span>&gt;</span>Block 3<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;item color-4&quot;</span>&gt;</span>Block 4<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span> {
   <span class="hljs-attribute">background</span>: <span class="hljs-number">#444444</span>;
   <span class="hljs-attribute">color</span>: white;
   <span class="hljs-attribute">font</span>: <span class="hljs-number">15px</span>/<span class="hljs-number">1.51</span> system, -apple-system, <span class="hljs-string">&quot;.SFNSText-Regular&quot;</span>, <span class="hljs-string">&quot;San Francisco&quot;</span>, <span class="hljs-string">&quot;Roboto&quot;</span>, <span class="hljs-string">&quot;Segoe UI&quot;</span>, <span class="hljs-string">&quot;Helvetica Neue&quot;</span>, <span class="hljs-string">&quot;Lucida Grande&quot;</span>, sans-serif;
   <span class="hljs-attribute">margin</span>:<span class="hljs-number">0</span> auto;
   <span class="hljs-attribute">max-width</span>:<span class="hljs-number">600px</span>;
   <span class="hljs-attribute">padding</span>:<span class="hljs-number">20px</span>;
}
<span class="hljs-selector-class">.item</span> {
  <span class="hljs-attribute">border</span>:<span class="hljs-number">4px</span> solid white;
  <span class="hljs-attribute">height</span>:<span class="hljs-number">120px</span>;
  <span class="hljs-attribute">width</span>:<span class="hljs-number">100%</span>;
  <span class="hljs-attribute">margin-bottom</span>:<span class="hljs-number">50px</span>;
  <span class="hljs-attribute">background</span>:<span class="hljs-number">#333</span>;
  <span class="hljs-attribute">padding</span>:<span class="hljs-number">20px</span>;
}



<span class="hljs-selector-class">.color-1</span> { <span class="hljs-attribute">border-color</span>: <span class="hljs-number">#9BFFBB</span>}
<span class="hljs-selector-class">.color-2</span> { <span class="hljs-attribute">border-color</span>: <span class="hljs-number">#B9C6FF</span>}
<span class="hljs-selector-class">.color-3</span> { <span class="hljs-attribute">border-color</span>: <span class="hljs-number">#FFA3D8</span>}
<span class="hljs-selector-class">.color-4</span> { <span class="hljs-attribute">border-color</span>: <span class="hljs-number">#FF8E9B</span>}
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-comment">// Very simple example.</span>
<span class="hljs-comment">// Probably you would want to use a </span>
<span class="hljs-comment">// full-featured plugin like</span>
<span class="hljs-comment">// https://github.com/infinite-scroll/infinite-scroll/blob/master/jquery.infinitescroll.js</span>
$(<span class="hljs-built_in">document</span>).ready(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{

  <span class="hljs-comment">// Check every 200ms the scroll position</span>
  $(<span class="hljs-built_in">document</span>).on(<span class="hljs-string">&apos;scroll&apos;</span>, _.throttle(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    check_if_needs_more_content();
  }, <span class="hljs-number">300</span>));

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">check_if_needs_more_content</span>(<span class="hljs-params"></span>) </span>{     
    pixelsFromWindowBottomToBottom = <span class="hljs-number">0</span> + $(<span class="hljs-built_in">document</span>).height() - $(<span class="hljs-built_in">window</span>).scrollTop() -$(<span class="hljs-built_in">window</span>).height();

  <span class="hljs-comment">// console.log($(document).height());</span>
  <span class="hljs-comment">// console.log($(window).scrollTop());</span>
  <span class="hljs-comment">// console.log($(window).height());</span>
  <span class="hljs-comment">//console.log(pixelsFromWindowBottomToBottom);</span>


    <span class="hljs-keyword">if</span> (pixelsFromWindowBottomToBottom &lt; <span class="hljs-number">200</span>){
      <span class="hljs-comment">// Here it would go an ajax request</span>
      $(<span class="hljs-string">&apos;body&apos;</span>).append($(<span class="hljs-string">&apos;.item&apos;</span>).clone()); 

    }
  }
});
</code></pre>
<h3 id="requestanimationframe"><a name="requestanimationframe" class="anchor-navigation-ex-anchor" href="#requestanimationframe"><i class="fa fa-link" aria-hidden="true"></i></a>1.1.4. requestAnimationFrame</h3>
<p> <code>requestAnimationFrame</code>&#x662F;&#x53E6;&#x4E00;&#x79CD;&#x9650;&#x5236;&#x51FD;&#x6570;&#x6267;&#x884C;&#x6B21;&#x6570;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x53EF;&#x4EE5;&#x8BA4;&#x4E3A;&#x4E0E;<code>_.throttle</code>&#x7C7B;&#x4F3C;&#xFF0C;&#x4F46;&#x662F;&#x5176;&#x62E5;&#x6709;&#x66F4;&#x9AD8;&#x7684;&#x51C6;&#x786E;&#x5EA6;&#xFF0C;&#x56E0;&#x4E3A;&#x5176;&#x672C;&#x8EAB;&#x5C31;&#x662F;&#x4E3A;&#x4E86;&#x66F4;&#x597D;&#x7684;&#x7CBE;&#x786E;&#x5EA6;&#x800C;&#x751F;&#x7684;&#x539F;&#x751F;API&#x3002; </p>
<p><strong>requestAnimationFrame&#x7684;&#x4F18;&#x70B9;</strong></p>
<ul>
<li>&#x81F4;&#x529B;&#x4E8E;60fps&#xFF08;16ms&#x6BCF;&#x5E27;&#xFF09;&#xFF0C;&#x81EA;&#x5DF1;&#x53EF;&#x4EE5;&#x51B3;&#x5B9A;&#x6700;&#x597D;&#x7684;&#x6E32;&#x67D3;&#x65F6;&#x95F4;&#x3002;</li>
<li>&#x7B80;&#x5355;&#x4E14;&#x6807;&#x51C6;&#x7684;&#x539F;&#x751F;API&#xFF0C;&#x4E0D;&#x592A;&#x5BB9;&#x6613;&#x6539;&#x53D8;&#xFF0C;&#x51CF;&#x5C11;&#x7EF4;&#x62A4;&#x6210;&#x672C;&#x3002;</li>
</ul>
<p><strong>requestAnimationFrame&#x7684;&#x7F3A;&#x70B9;</strong></p>
<ul>
<li>&#x9700;&#x8981;&#x624B;&#x52A8;&#x5F00;&#x59CB;&#x6216;&#x53D6;&#x6D88;rAFs&#xFF0C;<code>.debounce</code> &#x6216; <code>.throttle</code>&#x5219;&#x4E0D;&#x9700;&#x8981;&#x3002;</li>
<li>&#x5982;&#x679C;tab&#x6CA1;&#x6709;&#x6FC0;&#x6D3B;&#xFF0C;&#x5219;&#x4E0D;&#x4F1A;&#x6267;&#x884C;&#xFF0C;&#x5373;&#x4F7F;&#x89E6;&#x53D1; Ascroll, mouse &#x6216;&#x8005; keyboard &#x7B49;&#x4E8B;&#x4EF6;&#x4E5F;&#x4E0D;&#x884C;.</li>
<li>&#x4E0D;&#x652F;&#x6301; IE9, Opera Mini &#x548C; &#x8001;&#x7248;&#x672C; Android.</li>
<li>&#x4E0D;&#x652F;&#x6301;<code>node.js</code>&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x80FD;&#x5728;&#x670D;&#x52A1;&#x7AEF;&#x4F7F;&#x7528;&#x3002;</li>
</ul>
<p><strong>requestAnimationFrame&#x5B9E;&#x4F8B;</strong></p>
<p>&#x4E0E;<code>_.throttle</code>&#x76F8;&#x6BD4;&#xFF0C;&#x540C;&#x65F6;&#x8BBE;&#x7F6E; 16ms&#xFF0C; &#x76F8;&#x540C;&#x7684;&#x6027;&#x80FD;&#x73AF;&#x5883;&#x4E0B;&#xFF0C;rAF &#x53EF;&#x4EE5;&#x5728;&#x66F4;&#x590D;&#x6742;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x62E5;&#x6709;&#x66F4;&#x597D;&#x7684;&#x7ED3;&#x679C;&#x3002;</p>
<h3 id="&#x56DB;&#x3001;&#x7ED3;&#x8BBA;"><a name="&#x56DB;&#x3001;&#x7ED3;&#x8BBA;" class="anchor-navigation-ex-anchor" href="#&#x56DB;&#x3001;&#x7ED3;&#x8BBA;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1.5. &#x56DB;&#x3001;&#x7ED3;&#x8BBA;</h3>
<p>&#x4E00;&#x822C;&#x6765;&#x8BF4;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x7684;JavaScript&#x51FD;&#x6570;&#x662F;&#x76F4;&#x63A5;&#x7ED8;&#x5236;&#x6216;&#x8005;&#x52A8;&#x753B;&#x67D0;&#x4E9B;&#x5C5E;&#x6027;&#xFF0C;&#x90A3;&#x4E48;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>requestAnimationFrame</code>&#xFF0C;&#x5728;&#x9700;&#x8981;&#x91CD;&#x65B0;&#x8BA1;&#x7B97;&#x5143;&#x7D20;&#x4F4D;&#x7F6E;&#x7684;&#x5730;&#x65B9;&#x4F7F;&#x7528;&#x3002;</p>
<p>&#x9700;&#x8981;&#x53D1;&#x8D77;<code>Ajax</code>&#x8BF7;&#x6C42;&#xFF0C;&#x6216;&#x8005;&#x51B3;&#x5B9A;&#x662F;&#x5426;&#x589E;&#x52A0;&#x6216;&#x5220;&#x9664;&#x4E00;&#x4E2A;&#x7C7B;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x53EF;&#x4EE5;&#x8003;&#x8651;&#x4F7F;&#x7528;<code>_.debounce</code> &#x6216;&#x8005;<code>_.throttle</code> &#x3002;</p>
<p>debounce: &#x5728;&#x4E8B;&#x4EF6;&#x88AB;&#x89E6;&#x53D1;x&#x79D2;&#x540E;&#x518D;&#x6267;&#x884C;&#x56DE;&#x8C03;&#xFF0C;&#x5982;&#x679C;&#x5728;&#x8FD9;x&#x79D2;&#x5185;&#x53C8;&#x88AB;&#x89E6;&#x53D1;&#xFF0C;&#x5219;&#x91CD;&#x65B0;&#x8BA1;&#x65F6;&#x3002;</p>
<p>throttle: &#x89C4;&#x5B9A;&#x5728;&#x4E00;&#x4E2A;&#x5355;&#x4F4D;&#x65F6;&#x95F4;&#x5185;&#xFF0C;&#x53EA;&#x80FD;&#x89E6;&#x53D1;&#x4E00;&#x6B21;&#x51FD;&#x6570;&#x3002;&#x5982;&#x679C;&#x8FD9;&#x4E2A;&#x5355;&#x4F4D;&#x65F6;&#x95F4;&#x5185;&#x89E6;&#x53D1;&#x591A;&#x6B21;&#x51FD;&#x6570;&#xFF0C;&#x53EA;&#x6709;&#x4E00;&#x6B21;&#x751F;&#x6548;&#x3002;</p>
<p>requestAnimationFrame: &#x8282;&#x6D41;&#x7684;&#x53E6;&#x5916;&#x4E00;&#x79CD;&#x9009;&#x9879;&#xFF0C;&#x5728;&#x51FD;&#x6570;&#x91CD;&#x65B0;&#x8BA1;&#x7B97;&#x6216;&#x8005;&#x6E32;&#x67D3;&#x5143;&#x7D20;&#x65F6;&#xFF0C;&#x9700;&#x8981;&#x5E73;&#x6ED1;&#x8FC7;&#x6E21;&#x6216;&#x8005;&#x52A8;&#x753B;&#x7684;&#x65F6;&#x5019;&#x4F7F;&#x7528;&#x3002; </p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; zhengyuliang all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x7F16;&#x5199;&#x65F6;&#x95F4;&#xFF1A;
2019-11-18 10:00:05
</span></footer></body></html>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="JS开发必须知道的41个技巧.html" class="navigation navigation-prev " aria-label="Previous page: JS开发必须知道的41个技巧">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="JavaScript数组求并集，交集和差集.html" class="navigation navigation-next " aria-label="Next page: JavaScript数组求并集，交集和差集">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"JavaScript之防抖Debounce和节流Throttle","level":"4.7","depth":1,"next":{"title":"JavaScript数组求并集，交集和差集","level":"4.8","depth":1,"path":"javaScript/JavaScript数组求并集，交集和差集.md","ref":"javaScript/JavaScript数组求并集，交集和差集.md","articles":[]},"previous":{"title":"JS开发必须知道的41个技巧","level":"4.6","depth":1,"path":"javaScript/JS开发必须知道的41个技巧.md","ref":"javaScript/JS开发必须知道的41个技巧.md","articles":[]},"dir":"ltr"},"config":{"plugins":["sectionx","splitter","tbfed-pagefooter","toggle-chapters","theme-comscore","-lunr","-search","search-plus","donate","anchor-navigation-ex","expandable-chapters","chapter-fold","expandable-chapters-small","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy zhengyuliang","modify_label":"该文件编写时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"chapter-fold":{},"pluginsConfig":{"anchor-navigation-ex":{"showLevel":false,"showGoTop":true,"isRewritePageTitle":true,"isShowTocTitleIcon":true,"tocLevel1Icon":"fa fa-hand-o-right","tocLevel2Icon":"fa fa-hand-o-right","tocLevel3Icon":"fa fa-hand-o-right"}},"livereload":{},"splitter":{},"donate":{"title":"","button":"赏","wechat":"https://gitee.com/htzyl/img_bed/raw/master/pay/weixin-min.jpg","alipay":"https://gitee.com/htzyl/img_bed/raw/master/pay/alipay-min.jpg","wechatText":"微信打赏","alipayText":"支付宝打赏"},"fontsettings":{"theme":"white","family":"sans","size":2},"sectionx":{"tag":"b"},"highlight":{},"anchor-navigation-ex":{"showLevel":true,"associatedWithSummary":true,"mode":"float","showGoTop":true,"printLog":false,"multipleH1":true,"float":{"floatIcon":"fa fa-navicon","showLevelIcon":false,"level1Icon":"","level2Icon":"","level3Icon":""},"pageTop":{"showLevelIcon":false,"level1Icon":"","level2Icon":"","level3Icon":""}},"theme-comscore":{},"expandable-chapters-small":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"toggle-chapters":{},"expandable-chapters":{},"search-plus":{}},"theme":"default","author":"zhengyuliang","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"记录技术文档内容","language":"zh-hans","gitbook":">=3.0.0","description":"记录Gitbook的配置和一些插件的使用","theme-default":{"showLevel":true}},"file":{"path":"javaScript/JavaScript之防抖Debounce和节流Throttle.md","mtime":"2019-11-18T02:00:05.232Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-01-06T09:44:01.178Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sectionx/sectionx.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

